package com.abp.controller;

import com.abp.entity.User;
import com.abp.service.UserService;
import com.abp.utils.RandomValidateCodeUtil;
import com.abp.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping("/api/author")
public class LoginController {


    @Autowired
    private UserService userService;


    @GetMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
        randomValidateCode.getRandcode(request, response);//输出验证码图片方法
    }


    /**
     * 系统登陆
     * @param request
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request){
        String sysCode=(String)request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
        if(StringUtils.isEmpty(sysCode)){
            return Result.reError("验证码已过期，请重新获取");
        }
        if(!request.getParameter("code").equalsIgnoreCase(sysCode)){
            return Result.reError("验证码输入不匹配，请从新输入");
        }
        String username = request.getParameter("username");
        if(StringUtils.isEmpty(username)){
            return Result.reError("账号不能为空");
        }
        String pwd =  request.getParameter("pwd");
        if(StringUtils.isEmpty(pwd)){
            return Result.reError("密码不能为空");
        }
        String type =  request.getParameter("type");
        if(StringUtils.isEmpty(type)){
            return Result.reError("登录角色不能为空");
        }
        //进行登录认证
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("pwd",pwd);
        queryWrapper.eq("type",type);
        User user=this.userService.getOne(queryWrapper);
        if(user==null){
            return Result.reError("账号密码错误");
        }
        request.getSession().setAttribute("user",user);
        return Result.reOk("登录成功");
    }


    /**
     * 系统注册
     * @param dto
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/register")
    @ResponseBody
    public Result register(User dto){
        //进行账号唯一认证
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",dto.getUsername());
        User user=this.userService.getOne(queryWrapper);
        if(user!=null){
            return Result.reError("用户名已存在，请重新更换！！！");
        }
        dto.setCreatedAt(new Date());
        this.userService.save(dto);
        return Result.reOk("注册成功");
    }

    /**
     * 获取当前登录的用户信息
     * @param request
     * @return
     */
    @RequestMapping(value="/get")
    @ResponseBody
    public Result get(HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        if(user!=null){
            return Result.reOk("查询成功",user);
        }else{
            return Result.reError("查询失败");
        }

    }

    /**
     * 资源路径
     */
    public String view="/view/admin";

    /**
     * 进入首页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return view+"/index";
    }

    /**
     * 进入首页
     * @return
     */
    @RequestMapping("/welcome")
    public String welcome(){
        return view+"/welcome";
    }



}
